<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文章添加--layui后台管理模板</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="../../layui/css/layui.css" media="all" />

<script src="../../js/layui/layui.js"></script>
<link rel="stylesheet" href="../../css/font_eolqem241z66flxr.css"
	media="all" />
</head>
<body class="childrenBody">
	<form class="layui-form" id="Form" lay-filter="Form">
	<input type="hidden" name="pdtId">
		<div class="layui-form-item">
			<label class="layui-form-label"><span style="color: red;">*
			</span>商品名称</label>
			<div class="layui-input-inline">
				<input type="text" class="layui-input newsName" name="pdtName"
					lay-verify="required" placeholder="请输入名称">
			</div>
			<label class="layui-form-label"><span style="color: red;">*
			</span>商品价格</label>
			<div class="layui-input-inline">
				<input type="text" class="layui-input newsName" name="price"
					lay-verify="required" placeholder="请输入价格">
			</div>
			
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label"><span style="color: red;">* </span>商品主照片</label>
			<div class="layui-input-block">
				<button type="button" class="layui-btn" id="upload">
					<i class="layui-icon">&#xe67c;</i>上传图片
				</button>
			</div>
			<div  class="layui-input-block" style="margin-top: 20px;">
			<img id="imgSrc" style="width: 120px" src="" /><br />
			 <input type="hidden" name="pdtImg" id="picName" class="layui-input" />
			</div>
			
			<label class="layui-form-label">商品评级</label>
			<div class="layui-input-inline">
				<input type="text" class="layui-input newsName" name="discount"
					 placeholder="请输入评级">
			</div>
		</div>
		
		<div class="layui-form-item">
			<label class="layui-form-label"><span style="color: red;">*
			</span>所属类别</label>
			<div class="layui-input-inline">
				<input type="text" class="layui-input newsName" name="pdtClassId"
					lay-verify="required" placeholder="请选择">
			</div>
			<label class="layui-form-label"><span style="color: red;">*
			</span>是否上架</label>
			<div class="layui-input-inline">
				<input type="radio" name="putaway" value="是" title="是"> 
				<input type="radio" name="putaway" value="否" title="否 " checked> 
			</div>
		</div>
		
		<div class="layui-form-item">
			<label class="layui-form-label"><span style="color: red;">*
			</span>上架日期</label>
			<div class="layui-input-inline">
				<input type="text" class="layui-input newsName" name="dateTime"
					id="dateTime">
			</div>
			<label class="layui-form-label">权重</label>
			<div class="layui-input-inline">
				<input type="text" class="layui-input newsName" name="pdtWeight"
					lay-verify="required">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">商品详情</label>
			<textarea name="pdtTest" required lay-verify="required" placeholder="请输入" class="layui-textarea"></textarea>
		</div>
		<div class="layui-input-block">
			<button class="layui-btn" lay-submit="" lay-filter="setProduct">立即提交</button>
			<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		</div>
	</form>
	<script type="text/javascript">
	layui.use([ 'upload', 'layer' ,'form'], function() {
		var upload = layui.upload, $ = layui.$,form=layui.form, layer = layui.layer;
		
		 var data=JSON.parse(sessionStorage.getItem("product"));
		 form.val("Form",{
				"pdtId":data.pdtId,
				"pdtName":data.pdtName,
				"price":data.price,
				"discount":data.discount,
				"pdtTest":data.pdtTest,
				"pdtClassId":data.pdtClassId,
				"putaway":data.putaway,
				"dateTime":data.dateTime,
				"pdtWeight":data.pdtWeight,
			});
			$("#imgSrc").attr("src",
					"../../../"+data.pdtImg);
		  //上传图片
   		 var uploadInst = upload.render({
   				elem : '#upload', //绑定元素
   				url : 'http://localhost:8080/Greenleaf/uploadImage/upload',
   				done : function(res) {
   					if (res.success) {
   						//把返回的图片名，放到img标记的src里面
   						$("#imgSrc").attr("src",
   								"../../../images/" + res.remark);
   						//把返回的图片名，保存在隐藏域里面
   						$("#picName").val("images/"+res.remark);
   						layer.msg('新图片上传成功', {
   							icon : 6,
   							time : 1500
   						});
   					}
   				},
   				error : function() {
   					layer.msg("图片上传失败");
   				}
   			});
		form.on('submit(setProduct)',function(data){
			$.post(
					'http://localhost:8080/Greenleaf/product/update',
							$("#Form").serialize(),function(res){
						if (res) {
							
							var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
					        setTimeout(function(){
					            top.layer.close(index);
								top.layer.msg("修改成功！");
					 			layer.closeAll("iframe");
						 		//刷新父页面
						 		parent.location.reload();
					        },1000);
						}else{
							var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
					        setTimeout(function(){
					            top.layer.close(index);
								top.layer.msg("修改失败！");
					 			
					        },1000);
						}
					},"text"
			)
			return false;
		})
	});
	</script>

</body>
</html>